﻿using IotClientApp.Server.Services;
using Microsoft.Extensions.DependencyInjection;

namespace IotClientApp.Server.Host
{
    public class StartIotHost : IHostedService
    {
        private readonly IServiceScopeFactory _serviceScopeFactory;

        public StartIotHost(IServiceScopeFactory serviceScopeFactory)
        {
            _serviceScopeFactory = serviceScopeFactory;
        }
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            using var scope = _serviceScopeFactory.CreateScope();
            var _deviceService = scope.ServiceProvider.GetRequiredService<IDeviceService>();
            var devices = await _deviceService.GetDevices();
            foreach (var device in devices)
            {
                await _deviceService.StartDevice(device);
            }
        }

        public Task StopAsync(CancellationToken cancellationToken)
        {
            throw new NotImplementedException();
        }
    }
}